import { _decorator, Component, Node, view, Collider2D, Contact2DType, IPhysics2DContact, director, Director } from 'cc';
import { ItemType, GameEventType } from '../Event/EnumDefine';
import { EventManager } from '../Event/EventManager';
import { BuffDataType, DataConfig } from '../Config/DataConfig';
import Common from '../Config/Common';
const { ccclass, property } = _decorator;

@ccclass('Item')
export class Item extends Component {
    /** 道具类型 */
    type: ItemType = ItemType.ACT;
    /** 当前道具buff数据 */
    buffData: BuffDataType;
    /** 是否在碰撞后已处理销毁，不在此组件内检测超出屏幕销毁 */
    isCollisionDestroy:boolean = false;

    /** 此道具是需要每帧移动碰撞到主角，还是直接快速飞到主角身上 */
    isFlyToPlayer = false;

    itemSpeed = 10; // 道具下落速度（像素/秒）

    onLoad(): void {
        EventManager.instance.registerEvent(GameEventType.Change_Roll_Speed, (speed: number)=>{
            this.itemSpeed = speed;
        }, this);
    }
    onEnable() {
        // 监听碰撞事件
        let collider = this.getComponent(Collider2D);
        if (!this.isFlyToPlayer && collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        }else{
            // console.warn("Item Node Not add Collider2D Component.");
        }
    }
    onDisable() {
        // 取消监听碰撞事件
        const collider = this.getComponent(Collider2D);
        if (!this.isFlyToPlayer && collider) {
            collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            collider.off(Contact2DType.END_CONTACT, this.onEndContact, this);
        }
    }

    onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体开始接触时被调用一次
        const selfNode = selfCollider.node;
        const otherNode = otherCollider.node;
        if (otherNode.name === 'PlayerNode') {
            this.onHandleCollider(otherNode, selfNode)
        }else if (selfNode.name === "PlayerNode") {
            this.onHandleCollider(selfNode, otherNode)
        }
    }
    onEndContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {}

    /** 处理碰撞 */
    onHandleCollider(blockNode:Node, bulletNode:Node)
    {
        EventManager.instance.triggerEvent(GameEventType.Item_Buff, {itemType: this.type, buffData: this.buffData})
        this.isCollisionDestroy = true;
        // 等待物理回调销毁
        director.once(Director.EVENT_AFTER_PHYSICS, ()=>{
            if(bulletNode){
                bulletNode.destroy();
            }
        })
    }

    update(deltaTime: number) {
        //如果此道具是飞到主角身上，就不会发生碰撞响应及位置每帧更新
        if(this.isFlyToPlayer){
            return;
        }
        if(this.isCollisionDestroy){
            return;
        }
        // 更新活动道具的位置
        const item = this.node
        const newY = item.position.y - this.itemSpeed * deltaTime; // 计算新的y坐标
        item.setPosition(item.position.x, newY); // 更新道具位置

        // 如果道具超出屏幕下方，移除道具
        if (newY < Common.instance.getBottomY()) {
            item.destroy(); // 销毁道具
        }
    }

}
